package cn.wolfcode.web.controller;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/password")
public class PasswordController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 跳转修改密码页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/updatePassword")
    public String updatePassword(Model model,Long id){
        model.addAttribute("id",id);
        return "/password/updatePwd";
    }
    /**
     * 修改密码
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(Long id,String oldPassword,String newPassword) {
        employeeService.updatePassword(id,oldPassword, newPassword);
        return new JsonResult(true, "密码修改成功，请重新登录");
    }

    @RequestMapping("/resetPassword")
    public String resetPassword(Model model,Long id){
        Employee employee = employeeService.get(id);
        model.addAttribute("employee",employee);
        return "/password/resetPwd";
    }
    /**
     *
     *  重置密码
     */
    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(Long id,String newPassword){
        Employee employee = UserContext.getCurrentUser();
        employeeService.updatePassword(id, newPassword);
        return new JsonResult(true,"密码重置成功");
    }

}
